package com.mouse.magent.common.handler;

import com.mouse.magent.common.codec.MessageDecoder;
import com.mouse.magent.common.codec.MessageEncoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;

public class BaseChannelInitializer extends ChannelInitializer {
    private BaseHandler handler;

    public BaseChannelInitializer(BaseHandler handler) {
        this.handler = handler;
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
                new IdleStateHandler(60, 30, 0),
                new MessageDecoder(), new MessageEncoder(), handler);
        handler.getChannelGroup().add(channel);
    }
}
